package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.PermissionQueryDto;
import com.group.eams.pojo.entity.Permission;
import com.group.eams.pojo.entity.Permission;
import com.group.eams.pojo.vo.PermissionListVo;
import com.group.eams.service.PermissionService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 权限表(Permission)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:29
 */
@RestController
@RequestMapping("/permission")
public class PermissionController  {
    /**
     * 服务对象
     */
    @Autowired
    private PermissionService permissionService;

    @RequestMapping(value = "/findPermissionByRoleId",method = RequestMethod.GET)
    @ApiOperation(value = "根据角色id查询对应的权限")
    public Result findPermissionByRoleId(@RequestParam Long roleId){
        List<Permission> permissionList = permissionService.findPermissionIdRoleId(roleId);
        return Result.buildSuccess(permissionList);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value = "权限列表",httpMethod = "POST")
    public Result list(@RequestBody PermissionQueryDto permissionQueryDto){

        //1.准备分页条件
        Page<PermissionListVo> page=new Page<>(permissionQueryDto.getPage(),permissionQueryDto.getLimit());


        //3.查询分页数据
        Page<PermissionListVo> permissionByQueryDto = permissionService.findPermissionByQueryDto(page, permissionQueryDto);

        //返回结果
        return Result.buildSuccess(permissionByQueryDto.getTotal(),permissionByQueryDto.getRecords());
    }

    @RequestMapping(value = "/find",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找权限")
    public Result find(@RequestParam Long id){
        Permission permission = permissionService.getById(id);
        return Result.buildSuccess(permission);
    }
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value = "查找所有权限")
    public Result findAll(){
        List<Permission> permissionList = permissionService.list();
        return Result.buildSuccess(permissionList);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation("添加权限")
    public Result add(@RequestBody Permission permission){
        boolean result = permissionService.save(permission);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation("修改权限")
    public Result update(@RequestBody Permission permission){
        boolean result = permissionService.updateById(permission);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ApiOperation("根据id删除权限")
    public Result delete(@RequestParam Long id){
        boolean result = permissionService.removeById(id);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }


    @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
    @ApiOperation("批量删除权限")
    public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){
        boolean result = permissionService.removeByIds(batchDeleteDto.getIds());
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }
}

